﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Region.Forms
{
    public partial class ProcurorListForm : Form
    {
        private Boolean m_Selectable;
        private Int32 m_SelectProcurorID;
        public Boolean Selectable { get { return m_Selectable; } set { m_Selectable = value; SelectButton.Visible = m_Selectable; } }
        public Int32 SelectProcurorID { get { return m_SelectProcurorID; } }
        private Engine m_engine;
        private ProcurorList m_procurorList;
        Properties.Settings m_ps;
        public ProcurorListForm(Engine engine)
        {
            m_engine = engine;
            m_ps = Properties.Settings.Default;
            m_procurorList = new ProcurorList(m_engine);       

            InitializeComponent();
            Int32 MinYear, MaxYear;
            m_procurorList.MinMaxProcurorYear(out MinYear, out MaxYear);
            for (Int32 Y = MinYear; Y <= MaxYear; Y++)
                Year.Items.Add(new Utils.ListItemValue(Y.ToString(), Y));
            Year.SelectedItem = new Utils.ListItemValue(DateTime.Now.Year.ToString(), DateTime.Now.Year);
            LineInPage.Text = m_ps.KontReestrLineInPage.ToString();
            this.DialogResult = DialogResult.Cancel;
            m_SelectProcurorID = 0;
        }
        private void ReestrList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void Filter_Click(object sender, EventArgs e)
        {
            this.ReestrList.Items.Clear();
            m_procurorList.setWhere("LineInPage", LineInPage.Text);
            m_procurorList.setWhere("Find", Find.Text);
            /*m_rasporList.setWhere("RasporYear", RasporYear.Text);
            
            m_rasporList.setWhere("RasporNumStart", RasporNumStart.Text);
            m_rasporList.setWhere("RasporNumEnd", RasporNumEnd.Text);
            m_rasporList.setWhere("IspUserID", null); for (int i = 0; i < m_IspUserFIO.Length; i++) if (m_IspUserFIO[i] == IspUserList.Text) { m_rasporList.setWhere("IspUserID", m_IspUserID[i]); break; }
            m_rasporList.setWhere("ParentOtdelID", OtdelList.SelectItemID());*/
            m_procurorList.Filter();
            Int32 Num = m_procurorList.NumLines;
            for (int i = 0; i < Num; i++)
            {
                ProcurorList.Line line = m_procurorList.getLine(i);
                ListViewItem R = new ListViewItem(line.ProcurorID.ToString());
                R.Tag = line.ProcurorID;
                R.SubItems.Add(line.ProcurorNum.ToString());
                R.SubItems.Add(line.IspFIO);
                if (line.Start == new DateTime(1970, 1, 1)) R.SubItems.Add("[Не указана]"); else R.SubItems.Add(line.Start.ToShortDateString());
                R.SubItems.Add(line.Comment);
                if (line.Start == new DateTime(1970, 1, 1)) R.ForeColor = Color.Red;
                //if (line.) R.Group = this.ReestrList.Groups["My"]; else R.Group = this.ReestrList.Groups["All"];
                this.ReestrList.Items.Add(R);
            }
        }

        private void ProcurorListListForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                m_ps.KontReestrLineInPage = Int32.Parse(LineInPage.Text);
                m_ps.Save();
            }
            finally { }
        }

        private void ReestrList_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            m_SelectProcurorID = 0;
            try
            {
                if (ReestrList.SelectedItems.Count > 0)
                {
                    m_SelectProcurorID = (int)ReestrList.SelectedItems[0].Tag;
                    //this.DialogResult = DialogResult.OK;
                    SelectButton.Enabled = true;
                    Edit.Enabled = true;
                    return;
                }
            }
            catch{}
            //this.DialogResult = DialogResult.Cancel;
            m_SelectProcurorID = 0;
            SelectButton.Enabled = false;
            Edit.Enabled = false;
        }

        private void Select_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            /*ImportEgrip Import = new ImportEgrip(m_engine);            
            Import.ShowDialog();
            if (Import.KontID > 0)
            {
                KontForm KF = new KontForm(m_engine, Import.KontID);
                KF.ShowDialog();
                Kont K = m_engine.getKont(Import.KontID);
                Find.Text = K.OGRN;
                Filter_Click(this, null);
            }*/
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            if (m_SelectProcurorID > 0)
            {
                var KF = new Forms.ProcurorForm(m_engine, m_SelectProcurorID);
                this.Visible = false;
                KF.ShowDialog();
                this.Visible = true;
            }
        }

        private void ReestrList_DoubleClick(object sender, EventArgs e)
        {
            if (m_SelectProcurorID > 0)
            {
                var KF = new Forms.ProcurorForm(m_engine, m_SelectProcurorID);
                this.Visible = false;
                KF.ShowDialog();
                this.Visible = true;
            }
        }
    }
}
